package com.mybatisplus.demo.base;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @CLASSNAME CollectingResult
 * @DESCRIPTION 结果收集
 * @AUTHOR chen
 * @DATE 2019-10-28 21:57
 **/
public class CollectingResult {

    private static Stream <String> noVowels() throws IOException {
        String content = new String( Files.readAllBytes( Paths.get( "e:\\profile\\word.txt" ) ), StandardCharsets.UTF_8 );
        List <String> wordList = Arrays.asList( content.split( "\\PL+" ) );
        Stream <String> words = wordList.stream();
        return words.map( s -> s.replaceAll( "[Photo/Agencies]", "" ) );
    }

    private static <T> void show(String label, Set <T> set) {
        System.out.println( label + ": " + set.getClass().getName() );
        System.out.println( "[" + set.stream().limit( 10 ).map( Object::toString ).collect( Collectors.joining( "," ) ) + "]" );
    }

    public static void main(String[] args) throws IOException {

        Iterator <Integer> iter = Stream.iterate( 0, n -> n + 1 ).limit( 10 ).iterator();

        while (iter.hasNext()) System.out.println( iter.next() );

        Object[] numbers = Stream.iterate( 0, n -> n + 1 ).limit( 10 ).toArray();
        System.out.println( "numbers:" + Arrays.toString( numbers ) );


        try {
            Integer num = (Integer) numbers[0];
            System.out.println( "num" + num );
            System.out.println( "--------" );
//            Integer[] numbers1 = (Integer[]) numbers;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println( e );
        }

        Integer[] numbers2 = Stream.iterate( 0, n -> n + 1 ).limit( 10 ).toArray( Integer[]::new );
        System.out.println( "Integer array:" + Arrays.toString( numbers2 ) );

        Set <String> noVowelSet = noVowels().collect( Collectors.toSet() );
        show( "noVowelSet", noVowelSet );

        TreeSet <String> noVowelSetTreeSet = noVowels().collect( Collectors.toCollection( TreeSet::new ) );
        show( "noVowelSetTreeSet", noVowelSetTreeSet );

        String result = noVowels().limit( 10 ).collect( Collectors.joining() );
        System.out.println( "result:" + result );

        result = noVowels().limit( 10 ).collect( Collectors.joining( "," ) );
        System.out.println( "result join '---'" + result );

        IntSummaryStatistics summaryStatistics = noVowels().collect( Collectors.summarizingInt( String::length ) );

        double avg = summaryStatistics.getAverage();
        double max = summaryStatistics.getMax();
        System.out.println( "avg:" + avg );
        System.out.println( "max:" + max );

        System.out.println( "foreach" );
        noVowels().limit( 10 ).forEach( System.out::println );

        //todo
        Stream <Locale> localeStream = Stream.of( Locale.getAvailableLocales() );
        Map <String, Set <String>> setMap = localeStream.collect(
                Collectors.toMap( Locale::getDisplayCountry,
                l -> Collections.singleton( l.getDisplayLanguage() ), (a, b) -> {
            Set <String> union = new HashSet <>(a);
            union.addAll( b );
            return union;
        } ) );

        System.out.println(setMap);

        List<Integer> list = Arrays.asList(1,2,9,5,3,6,7);
        Stream<Integer> stream = Stream.of(1,2,9,5,3,6,7);
        IntSummaryStatistics statistics = stream.collect(Collectors.summarizingInt(Integer::new));
        System.out.println("sum:" + statistics.getSum());
        System.out.println("min:" + statistics.getMin());
        System.out.println("avg:" + statistics.getAverage());

    }
}
